package com.lightkits.wms.domain.deliveryorder;

import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.logging.Logger;
import com.lightkits.framework.common.logging.LoggerFactory;
import com.lightkits.framework.common.utility.date.DateUtil;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.soa.common.pipeline.session.SessionInfo;
import com.lightkits.framework.soa.hzero.executor.ServiceExecutor;
import com.lightkits.wms.database.dao.WmsDeliveryOrderBaseMapper;
import com.lightkits.wms.database.model.BdDeliveryOrderModel;
import com.lightkits.wms.dto.deliveryorder.QueryDeliveryExeRequestType;
import com.lightkits.wms.dto.deliveryorder.QueryDeliveryExeResponseType;
import com.lightkits.wms.dto.deliveryorder.model.DeliveryViewDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


/**
 * QueryDeliveryExeExecute
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/2/28 12:49
 */
@Component
public class QueryDeliveryExeExecute implements ServiceExecutor<QueryDeliveryExeRequestType, QueryDeliveryExeResponseType> {
    private final WmsDeliveryOrderBaseMapper bdDeliveryOrderBaseMapper;

    private final Logger LOGGER = LoggerFactory.getLogger(QueryDeliveryExeExecute.class);

    @Autowired
    public QueryDeliveryExeExecute(WmsDeliveryOrderBaseMapper bdDeliveryOrderBaseMapper) {
        this.bdDeliveryOrderBaseMapper = bdDeliveryOrderBaseMapper;
    }

    @Override
    public QueryDeliveryExeResponseType service(QueryDeliveryExeRequestType request) {
        QueryDeliveryExeResponseType response = new QueryDeliveryExeResponseType();
        response.setSuccessful(true);

        List<DeliveryViewDTO> viewDTOS = new ArrayList<>();
        List<BdDeliveryOrderModel> model = bdDeliveryOrderBaseMapper.queryDeliveryOrderByParams(request.getDeliveryNumOrId(), request.getDeliveryStatus(), SessionInfo.languageCode());
        if (ObjectUtil.isEmpty(model)) {
            response.setDeliveryViewDTO(viewDTOS);
            return response;
        }

        model.forEach(target -> {
            viewDTOS.add(convertViewDto(target));
        });
        response.setDeliveryViewDTO(viewDTOS);
        return response;

    }

    private DeliveryViewDTO convertViewDto(BdDeliveryOrderModel model) {
        DeliveryViewDTO viewDTO = new DeliveryViewDTO();
        try {
            viewDTO.setPrimaryUom(model.getPrimaryUom());
            viewDTO.setPrimaryUomText(model.getPrimaryUomText());
            if (!ObjectUtil.isEmpty(model.getDemandDate())) {
                viewDTO.setDemandDate(DateUtil.toDateString(model.getDemandDate(), DateUtil.DEFAULT_DATE_PATTERN));
            }
            viewDTO.setDoNum(model.getDoNum());
            viewDTO.setDoId(model.getDoId());
            viewDTO.setCustomerItem(model.getCustomerItem());
            viewDTO.setCustomerName(model.getCustomerName());
            viewDTO.setCustomerId(model.getCustomerId());
            viewDTO.setMakeOrderQty(model.getMakeOrderQty());
            viewDTO.setExeQty(model.getExeQty());
            viewDTO.setOrgId(model.getOrgId());
            viewDTO.setItemDesc(model.getItemDesc());
            viewDTO.setItemCode(model.getItemCode());
            viewDTO.setItemId(model.getItemId());
            viewDTO.setDemandQty(model.getDemandQty());
            viewDTO.setOrderStatusText(model.getOrderStatusText());
            viewDTO.setDeliveryNum(model.getDelioveryNum());
            if (!ObjectUtil.isEmpty(model.getMakeOrderDate())) {
                viewDTO.setMakeOrderDate(DateUtil.toDateString(model.getMakeOrderDate(), DateUtil.DEFAULT_DATETIME_PATTERN));
            }
            viewDTO.setOrderStatus(model.getOrderStatus());
            viewDTO.setDeliveryId(model.getDeliveryId());
        } catch (Exception e) {
            LOGGER.error("convertViewDto.error", e);
            throw new LightKitsBizException(e);
        }
        return viewDTO;
    }
}
